//
//  UICollectionViewCell+Init.m
//  TableView
//
//  Created by liaozhenming on 16/5/16.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "UICollectionViewCell+Init.h"

#import <objc/runtime.h>

@interface UICollectionView(Init)

@property (nonatomic, strong) NSMutableArray *kk_identifiers;

@end

@implementation UICollectionView(Init)

+ (void)load {
    
    Method registerClass = class_getInstanceMethod([self class], NSSelectorFromString(@"registerClass:forCellWithReuseIdentifier:"));
    Method kk_registerClass = class_getInstanceMethod([self class], @selector(kk_registerClass:forCellWithReuseIdentifier:));
    method_exchangeImplementations(registerClass, kk_registerClass);
    
    Method registerXib = class_getInstanceMethod([self class], NSSelectorFromString(@"registerNib:forCellWithReuseIdentifier:"));
    Method kk_registerXib = class_getInstanceMethod([self class], @selector(kk_registerNib:forCellWithReuseIdentifier:));
    method_exchangeImplementations(registerXib, kk_registerXib);
}

- (void)kk_registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier {
    
    if (identifier != nil) {
        if (![self kk_hasRegisterIdentifier: identifier]) {
            NSMutableArray *identifiers = [NSMutableArray array];
            [identifiers arrayByAddingObjectsFromArray: self.kk_identifiers];
            [identifiers addObject: identifier];
            [self setKk_identifiers: identifiers];
        }
    }
    
    [self kk_registerClass: cellClass forCellWithReuseIdentifier: identifier];
}

- (void)kk_registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier {
    
    if (identifier != nil) {
        if (![self kk_hasRegisterIdentifier: identifier]) {
            NSMutableArray *identifiers = [NSMutableArray array];
            [identifiers arrayByAddingObjectsFromArray: self.kk_identifiers];
            [identifiers addObject: identifier];
            [self setKk_identifiers: identifiers];
        }
    }
    
    [self kk_registerNib: nib forCellWithReuseIdentifier: identifier];
}

- (BOOL)kk_hasRegisterIdentifier:(NSString*)identifier {
    
    for (NSString *item in self.kk_identifiers) {
        if ([item isEqualToString: identifier]) {
            return YES;
        }
    }
    return NO;
}

@dynamic kk_identifiers;
static const void *UICollectionViewRegisterIdentifiers = &UICollectionViewRegisterIdentifiers;
- (void)setKk_identifiers:(NSMutableArray *)kk_identifiers {
    objc_setAssociatedObject(self, UICollectionViewRegisterIdentifiers, kk_identifiers, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSMutableArray*)kk_identifiers {
    return objc_getAssociatedObject(self, UICollectionViewRegisterIdentifiers);
}

@end


#pragma mark -
@implementation UICollectionViewCell (Init)

+ (instancetype)kk_cellWithCollectionView:(UICollectionView*)collectionView indexPath:(NSIndexPath*)indexPath {
    return [self kk_cellWithCollectionView: collectionView indexPath: indexPath identifier: NSStringFromClass(self.class)];
}

+ (instancetype)kk_cellWithCollectionView:(UICollectionView*)collectionView indexPath:(NSIndexPath*)indexPath identifier:(NSString *)identifier {
    
    UICollectionViewCell* cell = nil;
    if (![collectionView kk_hasRegisterIdentifier: identifier]) {
        
        NSString *resoucePath = [[NSBundle mainBundle] resourcePath];
        NSArray *xibPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: resoucePath error: nil];
        if (xibPaths) {
            BOOL hasOwne = NO;
            NSString *fileName = NSStringFromClass(self.class);
            for (NSString *itemPath in xibPaths) {
                if ([itemPath hasPrefix: fileName]) {
                    hasOwne = YES;
                    break;
                }
            }
            if (hasOwne) {
                [collectionView registerNib: [UINib nibWithNibName: NSStringFromClass(self.class) bundle: [NSBundle mainBundle]] forCellWithReuseIdentifier: identifier];
                cell = [collectionView dequeueReusableCellWithReuseIdentifier: identifier forIndexPath: indexPath];
                return cell;
            }
        }
        [collectionView registerClass: self.class forCellWithReuseIdentifier: identifier];
//        NSString *xibPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.nib",NSStringFromClass(self.class)]];
//        if (![[NSFileManager defaultManager] fileExistsAtPath: xibPath]) {
//            [collectionView registerClass: self.class forCellWithReuseIdentifier: identifier];
//        }
//        else {
//            [collectionView registerNib: [UINib nibWithNibName: NSStringFromClass(self.class) bundle: [NSBundle mainBundle]] forCellWithReuseIdentifier: identifier];
//        }
    }
    cell = [collectionView dequeueReusableCellWithReuseIdentifier: identifier forIndexPath: indexPath];
    return cell;
}


@end
